<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_playlist extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();	
		//Load Model
		$this->load->model('administrator_playlist_model');
    }
	
	/*Content Management*/
	public function content_management($module=NULL,$arg=NULL)
	{					
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_playlist_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
						
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				
				$this->data['content'] = "administrator/playlist/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);	
				//Check Id exist
				$is_exist = $this->db->from('playlist')
				->where(array('playlist_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
									
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_playlist_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;

				//Load Jquery UI
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";
						
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";			
				
				$this->data['dataResult'] = $this->db
				->join('playlist_info as j','j.playlist_id=t.playlist_id','INNER')
				->get_where('playlist as t',array('t.playlist_id' => $id),1)
				->row();
				
				$this->data['dataPlaylist'] = "";
				if(!empty($this->data['dataResult']->playlist_song)):
					$playlist_arr = explode(':',$this->data['dataResult']->playlist_song);
					foreach($playlist_arr as $value):
						if(preg_match('/video_/',$value)):
							$this->data['dataPlaylist'][] = $this->db->select("video_id as media_id, CONCAT( 'video_',video_id ) as media_value, video_name as media_name")->get_where('video_info',array('video_id' => preg_replace('/video_/','',$value)),1)->row();
						else:
							$this->data['dataPlaylist'][] = $this->db->select('song_id as media_id, song_id as media_value, song_name as media_name')->get_where('song_info',array('song_id' => $value),1)->row();						
						endif;
					endforeach;
				endif;
	
				$this->data['content'] = "administrator/playlist/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_delete_image':
				$id = intval($arg);
				$this->administrator_playlist_model->ajax_delete_image($id);
			break;	
			case 'delete':
				$this->administrator_playlist_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "playlist_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array('t.playlist_id IS NOT NULL');				
				$playlist_name = addslashes($this->input->get('playlist_name'));
				$playlist_brief = addslashes($this->input->get('playlist_brief'));
				$playlist_type = addslashes($this->input->get('playlist_type'));
				$update_by = addslashes($this->input->get('update_by'));		
				if(!empty($playlist_name)) $searchCondition[] = "j.playlist_name LIKE '%".$playlist_name."%'";
				if(!empty($playlist_brief)) $searchCondition[] = "j.playlist_brief LIKE '%".$playlist_brief."%'";
				if(!empty($playlist_type)) switch($playlist_type):
					case 'You2Play':
						$searchCondition[] = "t.member_id IS NULL";
					break;
					case 'Member':
						$searchCondition[] = "t.member_id IS NOT NULL";
					break;
					case 'Hilight':
						$searchCondition[] = "t.is_hilight = '1'";
					break;
					case 'Recommend':
						$searchCondition[] = "t.is_recommend = '1'";
					break;
				endswitch;
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('playlist as t')
				->join('playlist_info as j','j.playlist_id=t.playlist_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('t.*,j.playlist_alias,j.playlist_name,j.playlist_brief')->join('playlist_info as j','j.playlist_id=t.playlist_id','LEFT')->order_by($orderCondition)->get_where('playlist as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				
				$this->data['content'] = "administrator/playlist/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}	
	
	/*Playlist Competition*/
	public function playlist_competition($module=NULL,$arg=NULL)
	{
		//Load Library
		$this->load->library('redis');

		//Load Datetime
		$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
		$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
		$this->data['js'][] = "jquery-ui-sliderAccess.js";	
		$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";
		
		/*Method Get*/
		if(isset($_GET) && !empty($_GET)):
			$playlist_activity_start =  $this->input->get('playlist_activity_start');
			$playlist_activity_end =  $this->input->get('playlist_activity_end');
			$playlist_reset_vote =  $this->input->get('playlist_reset_vote');
			
			if(!empty($playlist_activity_start)) $this->redis->set('dataPlaylistActivityStart', $playlist_activity_start);
			if(!empty($playlist_activity_end)) $this->redis->set('dataPlaylistActivityEnd', $playlist_activity_end);
			if(!empty($playlist_reset_vote)) $this->db->update('playlist',array('vote_activity' => 0),"member_id IS NOT NULL");		
		endif;	
		
		$this->data['dataPlaylistActivityStart'] = $this->redis->get('dataPlaylistActivityStart');
		$this->data['dataPlaylistActivityEnd'] = $this->redis->get('dataPlaylistActivityEnd');			
				
		/*Order Condition*/
		$orderby = $this->input->get('orderby');
		$sort = $this->input->get('sort');
		$orderCondition = "t.vote_activity DESC";
		if(!empty($orderby) && !empty($sort)):
			switch($orderby):
				default:
					$orderCondition = $orderby." ".$sort;
			endswitch;
		endif;
					
		/*Search Condition*/
		$searchCondition = array('t.member_id IS NOT NULL');
				
		/*Pager*/		
		$config['base_url'] = current_url().'?'.query_string(array('per_page'));
		$config['total_rows'] = $this->db->from('playlist as t')
		->join('playlist_info as j','j.playlist_id=t.playlist_id','INNER')
		->where(''.implode(' AND ',$searchCondition).'')
		->count_all_results();
		$config['per_page'] = 20;
		$config['num_links'] = 10;
		$config['page_query_string'] = TRUE;
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();
		$this->data['dataResult'] = $this->db->select('t.playlist_id,t.member_id,j.playlist_alias as fb_id,j.playlist_name,t.vote_activity')->join('playlist_info as j','j.playlist_id=t.playlist_id','LEFT')->order_by($orderCondition)->get_where('playlist as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
						
		$this->data['content'] = "administrator/playlist/playlist_competition";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
	}	
}